package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.JedisUtils;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.service.ScheduleService;
import com.cookor.poi.modules.phy.service.TOrderService;
import com.cookor.poi.utils.HttpUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/order"})
@ResponseBody
public class ApiOrderController extends ApiBaseController {
    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private ScheduleService scheduleService;

    public static final String lock = "ImLock";

    @RequestMapping({"/checkBook"})
    public ResultUtil checkBook(Integer bookId, String openId, Integer actionId, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            this.tOrderService.chechBook(bookId, openId, actionId);
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController checkBook error!", e);
        }
        return result;
    }

    @RequestMapping({"/getNo"})
    public ResultUtil getNo(Integer year, String date, Integer index, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            Integer tenantId = getTenantId(request);
            TOrder orderQuery = new TOrder();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date bookDate = sdf.parse(year.toString() + "-" + date);
            orderQuery.setBookDate(bookDate);
            orderQuery.setBookDateType(index);
            orderQuery.setTenantId(tenantId);
            List<TOrder> oldOrders = this.tOrderService.findList(orderQuery);
            result.putParams("no", Integer.valueOf(oldOrders.size() + 1));
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController getNo error!", e);
        }
        return result;
    }

    @RequestMapping({"/create"})
    public ResultUtil createOrder(Integer year, String date, Integer index, String title, String beginDate, String endDate, Integer patientId, String openId, String uuid, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            String clientIP = HttpUtils.getClientIP(request, new String[0]);
            if (JedisUtils.get(clientIP) != null) {
                result.setCode(ResultUtil.ERROR_CODE);
                result.setError("系统繁忙,请重试！");
                return result;
            }
            if (StringUtils.isBlank(uuid) || StringUtils.isBlank(date) || StringUtils.isBlank(openId)) {
                result.setCode(ResultUtil.ERROR_CODE);
                result.setError("非法请求！");
                return result;
            }
            String cacheUUID = JedisUtils.get("uuid");
            System.out.println("cacheUUID:" + cacheUUID);
            if (cacheUUID == null || !cacheUUID.equals(uuid)) {
                result.setCode(ResultUtil.ERROR_CODE);
                result.setError("验签异常！");
                return result;
            }
            JedisUtils.set(clientIP, "1", 30);
            Calendar c = Calendar.getInstance();
            if (c.get(11) <= 8) {
                result.setCode(ResultUtil.ERROR_CODE);
                result.setError("九点开始放号，请稍后再试！");
                return result;
            }
            this.scheduleService.synchronizedCreate(year, date, index, title, beginDate, endDate, patientId, openId, getTenantId(request));
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController createOrder error!", e);
        }
        return result;
    }

    @RequestMapping({"/list"})
    public ResultUtil orderList(Integer orderStatus, String openId, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            if (StringUtils.isBlank(openId))
                throw new Exception("参数错误");
            Integer tenantId = getTenantId(request);
            TOrder order = new TOrder();
            order.setStatus(orderStatus);
            order.setDelFlag("0");
            order.setOpenId(openId);
            Page<TOrder> page = this.tOrderService.findPage(new Page(1, -1), order);
            result.putParams("page", page);
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController orderList error!", e);
        }
        return result;
    }

    @RequestMapping({"/detail"})
    public ResultUtil orderDetail(Integer orderId, String openId, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            if (StringUtils.isBlank(openId))
                throw new Exception("参数错误");
            Integer tenantId = getTenantId(request);
            TOrder tOrder = this.tOrderService.get(orderId.toString());
            if (!tOrder.getOpenId().equals(openId))
                throw new Exception("传递信息有误");
            result.putParams("order", tOrder);
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController orderDetail error!", e);
        }
        return result;
    }

    @RequestMapping({"/cancelAppoint"})
    public ResultUtil cancelAppoint(Integer orderId, String openId, HttpServletRequest request) {
        ResultUtil result = getResult();
        try {
            if (StringUtils.isBlank(openId))
                throw new Exception("参数错误");
            Integer tenantId = getTenantId(request);
            this.tOrderService.cancelAppoint(tenantId, orderId, openId);
        } catch (Exception e) {
            result.setCode(ResultUtil.ERROR_CODE);
            result.setError(e.getMessage());
            this.logger.error("ApiOrderController cancelAppoint error!", e);
        }
        return result;
    }
}
